
#ifndef RTHTTPCHUNKEDDECODER_H
#define RTHTTPCHUNKEDDECODER_H

#include "RtHttpHead.h"
#include <vector>

class CRtString;

template <typename ParserT>
class CRtHttpChunkedDecoder : public CRtReferenceControlSingleThreadTimerDelete
{
public:
	enum DecoderState 
	{ 
		StateChunkSize, 
		StateChunkData, 
		StateTrailer, 
		StateEmptyLine, 
		StateEof 
	};

	CRtHttpChunkedDecoder(ParserT *pParser);
	~CRtHttpChunkedDecoder();

	// called by the HTTP channel to handle chunked content.
	BOOL HandleChunkedContent(
		LPCSTR aBuf, 
		DWORD aTotalLen, 
		CRtString &aBufOut,
		DWORD &aReadOut);

	BOOL HandleEveryChunkedContent(
		LPCSTR aBuf, 
		DWORD aTotalLen, 
		std::vector<CRtString> &ChunkContentList,
		DWORD &aReadOut);

	BOOL ReachedEOF();

private:
	DecoderState		m_nState;
	CRtString			m_strLineBuf;
	DWORD				m_uCurrentChunkSize;
	BOOL				m_bHaveChunkDataCR;
	CRtHttpHeaderArray	m_HeaderArray;

	//for every chunck
	CRtString	m_strLatestPartialChunkContent;

	ParserT  *m_pParser;
};


#include "RtHttpChunkedDecoder.cpp"

#endif // !RTHTTPCHUNKEDDECODER_H
